<html>
<head><meta charset="utf-8"><title>Type punning and &quot;active field&quot; of a union · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html">Type punning and &quot;active field&quot; of a union</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="169291602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169291602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169291602">(Jun 29 2019 at 10:24)</a>:</h4>
<p>Some people think we are worse than C when it comes to unions :(<br>
<a href="https://www.reddit.com/r/rust/comments/c5w36i/brave_browser_from_the_inventor_of_javascript/es6vpdd/" target="_blank" title="https://www.reddit.com/r/rust/comments/c5w36i/brave_browser_from_the_inventor_of_javascript/es6vpdd/">https://www.reddit.com/r/rust/comments/c5w36i/brave_browser_from_the_inventor_of_javascript/es6vpdd/</a></p>



<a name="169291612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169291612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169291612">(Jun 29 2019 at 10:25)</a>:</h4>
<p>they cite in particular <a href="https://rust-lang.github.io/rfcs/1444-union.html#unions-and-undefined-behavior" target="_blank" title="https://rust-lang.github.io/rfcs/1444-union.html#unions-and-undefined-behavior">https://rust-lang.github.io/rfcs/1444-union.html#unions-and-undefined-behavior</a> as evidence for Rust tracking an "active field" of a union. may ewe could improve the wording there?</p>



<a name="169294014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294014">(Jun 29 2019 at 11:48)</a>:</h4>
<p>what does the nomicon say ?</p>



<a name="169294017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294017">(Jun 29 2019 at 11:48)</a>:</h4>
<p>AFAICT it's not worth it to update those RFCs, they are historic documents</p>



<a name="169294029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294029">(Jun 29 2019 at 11:49)</a>:</h4>
<p>the RFCs get merged into the reference, and that's maintained, so if anything, update the reference / nomicon</p>



<a name="169294308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294308">(Jun 29 2019 at 11:57)</a>:</h4>
<blockquote>
<p>AFAICT it's not worth it to update those RFCs, they are historic documents</p>
</blockquote>
<p>I am not sure if that is really clear to everyone</p>



<a name="169294310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294310">(Jun 29 2019 at 11:57)</a>:</h4>
<p>also the RFC there is not outdated, just badly worded</p>



<a name="169294354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294354">(Jun 29 2019 at 11:58)</a>:</h4>
<p>the nomicon does not have a section on unions</p>



<a name="169294581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294581">(Jun 29 2019 at 12:05)</a>:</h4>
<p>we have updated RFCs in the past</p>



<a name="169294589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169294589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169294589">(Jun 29 2019 at 12:06)</a>:</h4>
<p>but bandwidth-wise if the feature is already in the reference / nomicon it's often most worth it to update it there</p>



<a name="169300389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169300389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169300389">(Jun 29 2019 at 14:48)</a>:</h4>
<p>Most people just check the RFCs. It needs to be communicated better that the Reference is the actual authority.</p>



<a name="169302230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169302230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169302230">(Jun 29 2019 at 15:43)</a>:</h4>
<p>we <em>did</em> put this into the reference though: <a href="https://doc.rust-lang.org/stable/reference/items/unions.html" target="_blank" title="https://doc.rust-lang.org/stable/reference/items/unions.html">https://doc.rust-lang.org/stable/reference/items/unions.html</a></p>



<a name="169302235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169302235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169302235">(Jun 29 2019 at 15:43)</a>:</h4>
<blockquote>
<p>Unions have no notion of an "active field".</p>
</blockquote>



<a name="169303835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169303835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169303835">(Jun 29 2019 at 16:32)</a>:</h4>
<blockquote>
<p>Most people just check the RFCs. It needs to be communicated better that the Reference is the actual authority.</p>
</blockquote>
<p>I don't think we have any data about what most people check. But if I had to guess, I'd guess that most people check the book, then the libstd API docs, then nomicon, then reference, and only few people check RFCs (this group might still be hundreds of people though).</p>



<a name="169303852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169303852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169303852">(Jun 29 2019 at 16:33)</a>:</h4>
<blockquote>
<p>we did put this into the reference </p>
</blockquote>
<p>So the main unanswered question IMO is "why do some users go to the union RFC to learn the feature instead of going to the reference?".</p>



<a name="169305063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169305063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169305063">(Jun 29 2019 at 17:09)</a>:</h4>
<p>Hot take: people check whatever comes up as the first result on google.</p>



<a name="169305115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169305115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Shnatsel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169305115">(Jun 29 2019 at 17:10)</a>:</h4>
<p>Which may be any of these resources depending on the query.</p>



<a name="169305199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169305199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169305199">(Jun 29 2019 at 17:13)</a>:</h4>
<p>the reference has a disclaimer: "this is all WIP" etc. maybe the rendered RFCs should have similar disclaimer?</p>



<a name="169305212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169305212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169305212">(Jun 29 2019 at 17:13)</a>:</h4>
<p>the way it looks now, I'd rather trust the RFC than the ref, as the RFC does not come with any kind of disclaimer^^</p>



<a name="169308536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169308536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169308536">(Jun 29 2019 at 19:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I know you put it in the Reference, my point is that people don't <em>read</em> the reference much so they won't know what you put in it.</p>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> my data is anecdotal at best, but I've generally seen people in Discord cite <a href="http://docs.rs" target="_blank" title="http://docs.rs">docs.rs</a> and RFCs, but I've honestly never seen people cite the Reference.</p>
<blockquote>
<p>why don't users go to the reference?</p>
</blockquote>
<p>I would also guess that it's because of the big red disclaimer. I have never trusted the reference and basically immediately close the tab if a page sends me there.</p>
<p>That disclaimer needs to go, and as soon as possible, if you want people to trust the Reference. That's my take</p>



<a name="169308842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169308842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169308842">(Jun 29 2019 at 19:14)</a>:</h4>
<p>funny enough, the nomicon does not have such a disclaimer^^</p>



<a name="169308851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169308851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169308851">(Jun 29 2019 at 19:15)</a>:</h4>
<p>so I tend to agree, the reference disclaimer is a bit over-the-top, at least compared to what we do in other "books"</p>



<a name="169337879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169337879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169337879">(Jun 30 2019 at 12:18)</a>:</h4>
<blockquote>
<p>That disclaimer needs to go, and as soon as possible, if you want people to trust the Reference. That's my take</p>
</blockquote>
<p>But we don't want people to trust the reference because it is not normative</p>



<a name="169344086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169344086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169344086">(Jun 30 2019 at 15:34)</a>:</h4>
<p>The problem with people not trusting the Reference is that people end up not trusting the Reference</p>



<a name="169344147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169344147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169344147">(Jun 30 2019 at 15:36)</a>:</h4>
<p>well the problem is that they trust something else, something that's not really any more normative or up-to-date or accurate than the reference</p>



<a name="169344282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169344282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169344282">(Jun 30 2019 at 15:41)</a>:</h4>
<p>I don't think most people want documents that are normative now <em>and forever</em>. I think they usually want best effort descriptions of how things currently work even if it changes later.</p>



<a name="169344332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169344332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169344332">(Jun 30 2019 at 15:42)</a>:</h4>
<p>Being set in stone forever is nice and all, but it takes a while to get there (and that's okay).</p>



<a name="169362754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169362754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169362754">(Jul 01 2019 at 01:54)</a>:</h4>
<p>I emphatically do not want people to rely on whatever best effort non-normative things we say in the reference, in the UCG, or anywhere else. This ties our hands because eventually someone says "there's too much code to break".</p>



<a name="169362757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169362757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169362757">(Jul 01 2019 at 01:54)</a>:</h4>
<p>Explicit non-guarantees are a good thing tho</p>



<a name="169362769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169362769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169362769">(Jul 01 2019 at 01:55)</a>:</h4>
<p>as well as the compiler aggressively taking advantage of unspecified behavior as if it were UB</p>



<a name="169413628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169413628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169413628">(Jul 01 2019 at 17:22)</a>:</h4>
<blockquote>
<p>as well as the compiler aggressively taking advantage of unspecified behavior as if it were UB</p>
</blockquote>
<p>that's not at all easy</p>



<a name="169413678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169413678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169413678">(Jul 01 2019 at 17:23)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  note that C does not have the notion of an active field - only C++ has that</p>



<a name="169413805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169413805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169413805">(Jul 01 2019 at 17:24)</a>:</h4>
<p>are you sure? C also needs to rule out type-punning through unions unless the compiler can "see" that there is a union involved</p>



<a name="169414122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414122">(Jul 01 2019 at 17:29)</a>:</h4>
<p>there's that weird clause about only doing union-based type punning if the union is "in scope" or so. doesnt that require tracking the "active field" to define precisely?</p>



<a name="169414233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414233">(Jul 01 2019 at 17:30)</a>:</h4>
<p>C allows type punning through unions</p>



<a name="169414259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414259">(Jul 01 2019 at 17:31)</a>:</h4>
<p>only in very specific ways</p>



<a name="169414280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414280">(Jul 01 2019 at 17:31)</a>:</h4>
<p>just wrote this up the other day so I still have the example :D</p>



<a name="169414285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414285">(Jul 01 2019 at 17:31)</a>:</h4>
<p>this is UB in C:</p>
<div class="codehilite"><pre><span></span><span class="kt">float</span> <span class="nf">transmute_int_to_float_inner</span><span class="p">(</span><span class="kt">int</span> <span class="n">in</span><span class="p">,</span> <span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">float</span> <span class="o">*</span><span class="n">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="n">in</span><span class="p">;</span>
    <span class="k">return</span> <span class="o">*</span><span class="n">f</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// In a different file</span>

<span class="k">union</span> <span class="n">TypePun</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
    <span class="kt">float</span> <span class="n">f</span><span class="p">;</span>
<span class="p">};</span>

<span class="kt">float</span> <span class="nf">transmute_int_to_float</span><span class="p">(</span><span class="kt">int</span> <span class="n">in</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">union</span> <span class="n">TypePun</span> <span class="n">p</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">transmute_int_to_float_inner</span><span class="p">(</span><span class="n">in</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">p</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">p</span><span class="p">.</span><span class="n">f</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>



<a name="169414287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414287">(Jul 01 2019 at 17:31)</a>:</h4>
<p>it has to be UB, or else type-based alias analysis does not work</p>



<a name="169414335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414335">(Jul 01 2019 at 17:32)</a>:</h4>
<p><a href="https://port70.net/~nsz/c/c11/n1570.html#note95" target="_blank" title="https://port70.net/~nsz/c/c11/n1570.html#note95">https://port70.net/~nsz/c/c11/n1570.html#note95</a></p>



<a name="169414343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414343">(Jul 01 2019 at 17:32)</a>:</h4>
<p>"Warning: Potential Security Risk Ahead"</p>



<a name="169414371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414371">(Jul 01 2019 at 17:32)</a>:</h4>
<p>looks like they are still using GeoTrust certs</p>



<a name="169414383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414383">(Jul 01 2019 at 17:33)</a>:</h4>
<p>which means their admin hasnt checked their setup for many months. <em>that's</em> the real security risk^^</p>



<a name="169414389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414389">(Jul 01 2019 at 17:33)</a>:</h4>
<p>C does not have type-based alias analysis</p>



<a name="169414400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414400">(Jul 01 2019 at 17:33)</a>:</h4>
<p>uh. what? yes it does.</p>



<a name="169414401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414401">(Jul 01 2019 at 17:33)</a>:</h4>
<p>only C++ has that</p>



<a name="169414411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414411">(Jul 01 2019 at 17:33)</a>:</h4>
<p>that's what all the effective type stuff is for</p>



<a name="169414432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414432">(Jul 01 2019 at 17:33)</a>:</h4>
<p>I am very sure that GCC and clang both to TBAA in C</p>



<a name="169414436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414436">(Jul 01 2019 at 17:33)</a>:</h4>
<p>ah yes, you are right, i'm misremembering</p>



<a name="169414495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414495">(Jul 01 2019 at 17:34)</a>:</h4>
<p>so yes, that code is UB in C</p>



<a name="169414503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414503">(Jul 01 2019 at 17:34)</a>:</h4>
<p>those two pointers can be assumed not to alias</p>



<a name="169414545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414545">(Jul 01 2019 at 17:34)</a>:</h4>
<p>but that is kind of unrelated to type punning - C does support type punning, but your example has a TBAA violation</p>



<a name="169414575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414575">(Jul 01 2019 at 17:35)</a>:</h4>
<p>TBAA works by forbidding type punning :D</p>



<a name="169414582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414582">(Jul 01 2019 at 17:35)</a>:</h4>
<p>see see <a href="https://stackoverflow.com/questions/34616086/union-punning-structs-w-common-initial-sequence-why-does-c-99-but-not/34641113" target="_blank" title="https://stackoverflow.com/questions/34616086/union-punning-structs-w-common-initial-sequence-why-does-c-99-but-not/34641113">https://stackoverflow.com/questions/34616086/union-punning-structs-w-common-initial-sequence-why-does-c-99-but-not/34641113</a></p>



<a name="169414590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414590">(Jul 01 2019 at 17:35)</a>:</h4>
<p>In repr(Rust) union that doesn't work either, because creating a reference to a field would be an error - and you need to do that to figure the field offsets</p>



<a name="169414593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414593">(Jul 01 2019 at 17:35)</a>:</h4>
<p>the standard basically says that type punning through unions only works if the union is "visible"</p>



<a name="169414599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414599">(Jul 01 2019 at 17:35)</a>:</h4>
<p>nobody knows what that means</p>



<a name="169414606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414606">(Jul 01 2019 at 17:35)</a>:</h4>
<p>so repr(Rust) unions don't support type punning at all</p>



<a name="169414659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414659">(Jul 01 2019 at 17:36)</a>:</h4>
<p>that's entirely off-topic as the reasons are unrelated</p>



<a name="169414663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414663">(Jul 01 2019 at 17:36)</a>:</h4>
<p>C supports type punning by using the union as a transmute</p>



<a name="169414673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414673">(Jul 01 2019 at 17:36)</a>:</h4>
<p><code>union U { t }.v </code></p>



<a name="169414687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414687">(Jul 01 2019 at 17:36)</a>:</h4>
<p>the common interpretation of the "visible" thing is that you need to do something like <code>u.f</code> or so for the type-changing access -- the place needs to be computed "through" the union type</p>



<a name="169414699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414699">(Jul 01 2019 at 17:37)</a>:</h4>
<p>exactly</p>



<a name="169414709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414709">(Jul 01 2019 at 17:37)</a>:</h4>
<p>but that's a very restricted form of union-based type punning</p>



<a name="169414714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414714">(Jul 01 2019 at 17:37)</a>:</h4>
<p>the moment you take a pointer, you're out</p>



<a name="169414733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414733">(Jul 01 2019 at 17:37)</a>:</h4>
<p>not always</p>



<a name="169414740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414740">(Jul 01 2019 at 17:37)</a>:</h4>
<p>if there would be an agreed-upon operational semantics for C, that could be made precise; the standard weasels its way around that with this "visible" rule</p>



<a name="169414791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414791">(Jul 01 2019 at 17:38)</a>:</h4>
<p>but saying "TBAA violation" is the wrong way around. you have to <em>prove TBAA correct</em> by deriving it from what the standard says about effective types.</p>



<a name="169414827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414827">(Jul 01 2019 at 17:38)</a>:</h4>
<p>and effective types are not enough with unions, because of that clause that says that unions re-interpret the storage at the type used for reading. so this needs to be restricted to enable TBAA.</p>



<a name="169414861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414861">(Jul 01 2019 at 17:39)</a>:</h4>
<p>Let's make your example a bit worse:</p>
<div class="codehilite"><pre><span></span><span class="kt">float</span> <span class="nf">transmute_int_to_float_inner</span><span class="p">(</span><span class="kt">int</span> <span class="n">in</span><span class="p">,</span> <span class="k">union</span> <span class="n">TypePun</span><span class="o">*</span> <span class="n">u</span><span class="p">,</span>  <span class="kt">int</span> <span class="o">*</span><span class="n">x</span><span class="p">,</span> <span class="kt">float</span> <span class="o">*</span><span class="n">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>


<p>Can the compiler assume that <code>u</code> and e.g. <code>x</code> do not alias?</p>



<a name="169414885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414885">(Jul 01 2019 at 17:39)</a>:</h4>
<p>I'd say so, because they have different types.</p>



<a name="169414895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414895">(Jul 01 2019 at 17:39)</a>:</h4>
<p>C++ does it with active fields. C does it with a "visible" rule that is imprecise. a strict reading of the standard would make my code above okay if its all in one file as the union <em>type</em> is "visible" everywhere.</p>



<a name="169414983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169414983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169414983">(Jul 01 2019 at 17:40)</a>:</h4>
<p>i'm much more familiar with the C++ standard than with the C standard</p>



<a name="169415039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415039">(Jul 01 2019 at 17:40)</a>:</h4>
<p>I mean this discussion is not really interesting for Rust anyway^^ Rust doesnt need any of this mess of a "visible" rule or an active field because we dont want TBAA</p>



<a name="169415220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415220">(Jul 01 2019 at 17:43)</a>:</h4>
<p>Not having TBAA removes many headaches</p>



<a name="169415306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415306">(Jul 01 2019 at 17:44)</a>:</h4>
<p>But not all of them</p>



<a name="169415382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415382">(Jul 01 2019 at 17:45)</a>:</h4>
<p>For example, given:</p>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">union</span> <span class="nc">U</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">SmallType</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">LargeType</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">LargeType</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">SmallType</span><span class="o">&gt;</span><span class="p">());</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">u</span>: <span class="nc">U</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">SmallType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">u</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">_</span><span class="p">;</span><span class="w"></span>
<span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">LargeType</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="n">ptr</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">).</span><span class="n">add</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">write</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Is that ok ? I mean, technically the storage is there, but <code>*mut u8</code> was derived from a <code>&amp;mut SmallType</code>, and its accessing memory OOB of <code>SmallType</code>. Do we want to optimize based on that not happening?</p>



<a name="169415399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415399">(Jul 01 2019 at 17:45)</a>:</h4>
<p>(deleted)</p>



<a name="169415676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415676">(Jul 01 2019 at 17:48)</a>:</h4>
<p>Stacked Borrows says this is NOT okay because when casting a mutable ref to a raw ptr, only the bytes that this ref actually points to become "raw-accessible"</p>



<a name="169415698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415698">(Jul 01 2019 at 17:49)</a>:</h4>
<p>In C++ this is not ok either.</p>



<a name="169415723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415723">(Jul 01 2019 at 17:49)</a>:</h4>
<p>this is tracked at <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/134" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/134">https://github.com/rust-lang/unsafe-code-guidelines/issues/134</a></p>



<a name="169415741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415741">(Jul 01 2019 at 17:49)</a>:</h4>
<p>I dont know yet if this is practical, but relaxing it has to be done very carefully or else it will destroy optimizations</p>



<a name="169415744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415744">(Jul 01 2019 at 17:49)</a>:</h4>
<p>There I had to cast it to an <code>u8</code>, but I can make <code>SmallType == u8</code>, and then it's kind of a bit less obvious.</p>



<a name="169415806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415806">(Jul 01 2019 at 17:50)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="cp">#[repr(C)]</span><span class="w"> </span><span class="k">union</span> <span class="nc">U</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">LargeType</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">LargeType</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="kt">u8</span><span class="o">&gt;</span><span class="p">());</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">u</span>: <span class="nc">U</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">SmallType</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">u</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">_</span><span class="p">;</span><span class="w"></span>
<span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">LargeType</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">ptr</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">write</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169415827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415827">(Jul 01 2019 at 17:50)</a>:</h4>
<p>no, you should cast the <em>union ptr</em> to raw</p>



<a name="169415831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415831">(Jul 01 2019 at 17:50)</a>:</h4>
<p>then you can use that for either field</p>



<a name="169415835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415835">(Jul 01 2019 at 17:50)</a>:</h4>
<p>Yeah, I know, but it looks like an easy error to make.</p>



<a name="169415837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415837">(Jul 01 2019 at 17:50)</a>:</h4>
<p>And then you get UB.</p>



<a name="169415947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169415947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169415947">(Jul 01 2019 at 17:52)</a>:</h4>
<p>indeed, that's why I opened that issue</p>



<a name="169416220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416220">(Jul 01 2019 at 17:55)</a>:</h4>
<p>If we want to be able to use <code>getelementptr</code> with <code>inbounds</code> I don't see how to avoid being strict there.</p>



<a name="169416278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416278">(Jul 01 2019 at 17:56)</a>:</h4>
<p>hu?  dont see the connection</p>



<a name="169416304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416304">(Jul 01 2019 at 17:56)</a>:</h4>
<p>we could just say "it makes the entire allocation accessible to raw ptr"</p>



<a name="169416315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416315">(Jul 01 2019 at 17:56)</a>:</h4>
<p>the hard part is not destroying reference-based reorderings</p>



<a name="169416340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416340">(Jul 01 2019 at 17:56)</a>:</h4>
<p>I mean, the reason this rule exists in C++ is to allow using <code>getelementptr</code> with <code>inbounds</code></p>



<a name="169416380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416380">(Jul 01 2019 at 17:57)</a>:</h4>
<p>I thought that was the optimization that you had in mind. Was it something else?</p>



<a name="169416397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416397">(Jul 01 2019 at 17:57)</a>:</h4>
<blockquote>
<p>I mean, the reason this rule exists in C++ is to allow using <code>getelementptr</code> with <code>inbounds</code></p>
</blockquote>
<p>which rule?</p>



<a name="169416503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416503">(Jul 01 2019 at 17:58)</a>:</h4>
<p>That a pointer to an object (or sub-object) can only point to the addresses in range of <code>[object_addr, object_addr + size + 1]</code>.</p>



<a name="169416550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416550">(Jul 01 2019 at 17:58)</a>:</h4>
<blockquote>
<p>I thought that was the optimization that you had in mind. Was it something else?</p>
</blockquote>
<p>something like</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">))</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">raw</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="mi">1</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="n">x</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">14</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="c1">// lots of code, using raw, but not using x.</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// can be optimized to: return 14;</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="169416613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416613">(Jul 01 2019 at 17:59)</a>:</h4>
<blockquote>
<p>That a pointer to an object (or sub-object) can only point to the addresses in range of <code>[object_addr, object_addr + size + 1]</code>.</p>
</blockquote>
<p>if they would allow it to "snap" to a surrounding union, that would still permit <code>inbounds</code></p>



<a name="169416624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416624">(Jul 01 2019 at 17:59)</a>:</h4>
<p>I mean, that optimization is doable because you assume that <code>raw</code> cannot be used to access x.0</p>



<a name="169416658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416658">(Jul 01 2019 at 17:59)</a>:</h4>
<p>typically this is done by assuming that raw can only be used to access <code>x.1</code></p>



<a name="169416721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416721">(Jul 01 2019 at 18:00)</a>:</h4>
<p>C++ just forbids all pointer arithmetic that results in a pointer address that's not inbounds of x.1</p>



<a name="169416771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416771">(Jul 01 2019 at 18:00)</a>:</h4>
<p>I wasnt aware C++ forbids this. LLVM allows it.</p>



<a name="169416781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416781">(Jul 01 2019 at 18:00)</a>:</h4>
<p>it supports the one-past-end rule, so that you can write `for (; ptr != one_past_end; ++ptr)</p>



<a name="169416806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416806">(Jul 01 2019 at 18:00)</a>:</h4>
<p>I think de-facto-C++ will have to allow it too</p>



<a name="169416807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416807">(Jul 01 2019 at 18:00)</a>:</h4>
<p>LLVM allows it, unless you use <code>getlementptr inbounds</code></p>



<a name="169416832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416832">(Jul 01 2019 at 18:01)</a>:</h4>
<p>I would expect many people will do something like "take address of first element of an array / vector / whatever, and use that to access other elements"</p>



<a name="169416854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416854">(Jul 01 2019 at 18:01)</a>:</h4>
<blockquote>
<p>LLVM allows it, unless you use <code>getlementptr inbounds</code></p>
</blockquote>
<p>that is incorrect</p>



<a name="169416863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416863">(Jul 01 2019 at 18:01)</a>:</h4>
<p><code>inbounds</code> referes to the bounds <em>of the allocation</em></p>



<a name="169416884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416884">(Jul 01 2019 at 18:01)</a>:</h4>
<p>not of the subobject that you are indexing into</p>



<a name="169416891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169416891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169416891">(Jul 01 2019 at 18:01)</a>:</h4>
<p>LLVM allows this <em>with <code>inbounds</code></em></p>



<a name="169417008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417008">(Jul 01 2019 at 18:03)</a>:</h4>
<p>hmmm, am i misremembereing again ?</p>



<a name="169417032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417032">(Jul 01 2019 at 18:03)</a>:</h4>
<p>does <code>inbounds</code> allow passing a size ? one option does not, but another option does, let me look</p>



<a name="169417132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417132">(Jul 01 2019 at 18:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> <code>inrange &lt;ty&gt; idx</code></p>



<a name="169417168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417168">(Jul 01 2019 at 18:05)</a>:</h4>
<blockquote>
<p>If the inrange keyword is present before any index, loading from or storing to any pointer derived from the getelementptr has undefined behavior if the load or store would access memory outside of the bounds of the element selected by the index marked as inrange.</p>
</blockquote>



<a name="169417169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417169">(Jul 01 2019 at 18:05)</a>:</h4>
<p><code>inrange</code>... never seen that</p>



<a name="169417196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417196">(Jul 01 2019 at 18:05)</a>:</h4>
<p>interesting. I dont think we emit it anywhere currently?</p>



<a name="169417253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417253">(Jul 01 2019 at 18:06)</a>:</h4>
<p>and TBH this entire subobject thing is <em>really complicated</em> when you try to do it formally. so I am very happy we don't do it.</p>



<a name="169417254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417254">(Jul 01 2019 at 18:06)</a>:</h4>
<p>so with <code>getelementptr inbounds ... inrange &lt;ty&gt; idx</code> you say that at the idx within the allocation there is a <code>&lt;ty&gt;</code>, and that the resulting pointer cannot be used to access the allocation out of that type storage</p>



<a name="169417366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417366">(Jul 01 2019 at 18:07)</a>:</h4>
<p>so if we say that in <code>let a [0, 1, 2, 3]; let p = &amp;a[0];</code> the <code>p</code> and pointers derived from it can only be used to access the first i32, we can emit an <code>inrange i32 0</code>.</p>



<a name="169417461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417461">(Jul 01 2019 at 18:08)</a>:</h4>
<blockquote>
<p>The result of a pointer comparison or ptrtoint (including ptrtoint-like operations involving memory) involving a pointer derived from a getelementptr with the inrange keyword is undefined, with the exception of comparisons in the case where both operands are in the range of the element selected by the inrange keyword, inclusive of the address one past the end of that element. Note that the inrange keyword is currently only allowed in constant getelementptr expressions.</p>
</blockquote>



<a name="169417513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417513">(Jul 01 2019 at 18:09)</a>:</h4>
<p>the one past the end there allows the <code>for (; ptr != one_past_end; ++ptr)</code> case</p>



<a name="169417680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417680">(Jul 01 2019 at 18:11)</a>:</h4>
<blockquote>
<p>so if we say that in <code>let a [0, 1, 2, 3]; let p = &amp;a[0];</code> the <code>p</code> and pointers derived from it can only be used to access the first i32, we can emit an <code>inrange i32 0</code>.</p>
</blockquote>
<p>I agree... interesting how Stacked Borrows "replaces" an explicit model of subobjects here</p>



<a name="169417785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169417785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169417785">(Jul 01 2019 at 18:12)</a>:</h4>
<p>I wonder if we can similarly use Stacked Borrows to justify <code>inbounds</code>. I mean we already basically do for references. just not for raw pointers.</p>



<a name="169418572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418572">(Jul 01 2019 at 18:20)</a>:</h4>
<p>I think that would be really cool</p>



<a name="169418653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418653">(Jul 01 2019 at 18:21)</a>:</h4>
<p>I suggested for this reason that we dont do <code>inbounds</code> when a raw ptr is used</p>



<a name="169418661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418661">(Jul 01 2019 at 18:21)</a>:</h4>
<p>but <span class="user-mention" data-user-id="124289">@rkruppe</span> didnt like the idea ;)</p>



<a name="169418868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418868">(Jul 01 2019 at 18:24)</a>:</h4>
<p>I do like getting rid of the part that corresponds to "stay in the same (sub)object" part. I just don't want to lose the other aspect, that the offset is less than isize::MAX.</p>



<a name="169418935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418935">(Jul 01 2019 at 18:25)</a>:</h4>
<p>ah right you were mostly worried about overflows</p>



<a name="169418983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418983">(Jul 01 2019 at 18:25)</a>:</h4>
<p>making overflows for field accesses UB is fairly easy I think, Miri probably already does that</p>



<a name="169418988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169418988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169418988">(Jul 01 2019 at 18:25)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> does <code>inrange</code> allow any interesting optimizations ?</p>



<a name="169419021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169419021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169419021">(Jul 01 2019 at 18:25)</a>:</h4>
<p>but note the part I am worried about is not "stay in the same (sub)object", it's "stay in the same allocation"</p>



<a name="169419439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169419439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169419439">(Jul 01 2019 at 18:30)</a>:</h4>
<p>I parenthesized the "sub" part for a reason! And object/allocation is tomayto, tomahto</p>



<a name="169419464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169419464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169419464">(Jul 01 2019 at 18:30)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I don't know.</p>



<a name="169419955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type%20punning%20and%20%22active%20field%22%20of%20a%20union/near/169419955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Type.20punning.20and.20.22active.20field.22.20of.20a.20union.html#169419955">(Jul 01 2019 at 18:36)</a>:</h4>
<blockquote>
<p>I parenthesized the "sub" part for a reason! And object/allocation is tomayto, tomahto</p>
</blockquote>
<p>oh. not sure if that's how everyone uses the word. ;)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>